const db = wx.cloud.database()
const questionList = db.collection('question')
import Dialog from '@vant/weapp/dialog/dialog';
Page({

      /**
       * 页面的初始数据
       */
      data: {
        work_title: '',
        work_type: '',
        showtype: false,
        type: ['判断', '单选', '多选'],
        work_unit: '',
        showunit: false,
        unit: ['第一章 绪论', '第二章 线性表', '第三章 栈和队列',
          '第四章 串', '第五章 数组和广义表', '第六章 树与二叉树',
          '第七章 图', '第八章 查找', '第九章 排序'
        ],
        showitem: true,
        work_item1: '',
        work_item2: '',
        work_item3: '',
        work_item4: '',
        work_answer: '',
        showanswer: false,
        answer: [],
        work_Parse: '',
        work_knowledge: '',
        work_difficulty: ''
      },


      // 以是点击类型所执行的事件


      onDisplaytype() {
        this.setData({
          showtype: true
        });
      },
      onClosetype() {
        this.setData({
          showtype: false
        });
      },
      onChangetype(event) {
        let answer = this.data.answer

        const {
          picker,
          value,
          index
        } = event.detail;
        if (value == "单选" || value == "多选") {
          this.setData({
            showitem: false
          })
        } else {
          this.setData({
            showitem: true,
          })
        }
        this.setData({
          work_type: value
        })
      },

      // 以是点击章节所执行的事件
      onDisplayunit() {
        this.setData({
          showunit: true
        });
      },
      onCloseunit() {
        this.setData({
          showunit: false
        });
      },
      onChangetunit(event) {
        const {
          picker,
          value,
          index
        } = event.detail;
        this.setData({
          work_unit: value
        })
      },


      // 以是点击答案所执行的事件
      onDisplayanswer() {
        this.setData({
          showanswer: true
        });
        if (this.data.work_type == "判断") {
          this.setData({
            answer: ["正确", "错误"]
          })
        } else {
          this.setData({
            answer: [this.data.work_item1, this.data.work_item2, this.data.work_item3, this.data.work_item4]
          })
        }

      },
      onCloseanswer() {
        this.setData({
          showanswer: false
        });
      },
      onChangeanswer(event) {
        const {
          picker,
          value,
          index
        } = event.detail;
        this.setData({
          work_answer: value
        })
      },

      // 难度选择器
      ondifficulty(event) {
        this.setData({
          work_difficulty: event.detail,
        });
      },



      //弹窗确认事件事件


      updata(){

      Dialog.confirm({
            title: '提示',
            message: '确认上传？',
          })

          .then(() => {

              wx.showLoading({
                title: '稍等！',
                mask: true
              })
              let work_unit = this.data.work_unit
              let work_title = this.data.work_title
              let work_type = this.data.work_type
              let work_item1 = this.data.work_item1
              let work_item2 = this.data.work_item2
              let work_item3 = this.data.work_item3
              let work_item4 = this.data.work_item4
              let work_Parse = this.data.work_Parse
              let work_answer = this.data.work_answer
              let work_knowledge = this.data.work_knowledge
              let work_difficulty = this.data.work_difficulty
              console.log(work_unit, work_type, work_item1, work_item2, work_item3, work_item4, work_Parse, work_answer, work_knowledge, work_difficulty)
              db.collection('question').add({
                  data: {
                    unit: this.data.work_unit,
                    title: this.data.work_title,
                    type: this.data.work_type,
                    options: [this.data.work_item1, this.data.work_item2, this.data.work_item3, this.data.work_item4],
                    Parse: this.data.work_Parse,
                    answer: this.data.work_answer,
                    knowledge: this.data.work_knowledge,
                    difficulty: this.data.work_difficulty
                  }

                })
                .then(res => {
                  wx.hideLoading()
                })
                //错误时执行
                .catch(err => {

                })
              console.log("成功！")

              })

          .catch(() => {});
            },
            /**
             * 生命周期函数--监听页面加载
             */
            onLoad(options) {

            },

            /**
             * 生命周期函数--监听页面初次渲染完成
             */
            onReady() {

            },

            /**
             * 生命周期函数--监听页面显示
             */
            onShow() {

            },

            /**
             * 生命周期函数--监听页面隐藏
             */
            onHide() {

            },

            /**
             * 生命周期函数--监听页面卸载
             */
            onUnload() {

            },

            /**
             * 页面相关事件处理函数--监听用户下拉动作
             */
            onPullDownRefresh() {

            },

            /**
             * 页面上拉触底事件的处理函数
             */
            onReachBottom() {

            },

            /**
             * 用户点击右上角分享
             */
            onShareAppMessage() {

            }
          })